package com.sunlands.deskmate.channel;

import com.sunlands.deskmate.util.SpringUtils;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.stream.ChunkedWriteHandler;
import org.springframework.stereotype.Component;

/**
 * @author lishuai
 *
 */
@Component
public class ChildChannelHandler extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {

        WebSocketServerHandler webSocketServerHandler = SpringUtils.getApplicationContext().getBean(WebSocketServerHandler.class);
        ch.pipeline().addLast("http-codec", new HttpServerCodec())// // HttpServerCodec：将请求和应答消息解码为HTTP消息
                .addLast("aggregator", new HttpObjectAggregator(65535))// HttpObjectAggregator：将HTTP消息的多个部分合成一条完整的HTTP消息
                .addLast("http-chunked", new ChunkedWriteHandler()) //ChunkedWriteHandler：向客户端发送HTML5文件
                .addLast("handler", webSocketServerHandler);  // 配置通道处理  来进行业务处理
    }
}
